package com.pro.back.controller;

import com.pro.back.bean.common.ApiRes;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Author: jingwenjuan
 * @Description:
 */
@RestController
@RequestMapping("/system")
public class SystemController {

    @Value("${uploadPath}")
    private String uploadPath;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public ApiRes noDownUploadImg(@RequestParam("file") MultipartFile file) {
        String path = uploadPath;
        String resUrl = "http://localhost:9898/upload/";
        if (file.isEmpty()) {
            return new ApiRes("文件不能为空");
        }
        String fileName = file.getOriginalFilename();
        File file1 = new File(path);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        File dest = new File(path, fileName);
        try {
            file.transferTo(dest);
            fileName = resUrl+fileName;
            return new ApiRes(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ApiRes("上传失败");
    }
}
